﻿using Saisie_des_prélèvements.Models;
using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Saisie_des_prélèvements.ViewModels
{
    class ANALYSESViewModel : ViewModelBase
    {
        #region Properties
        private string analyze_code { get; set; }
        public string Code
        {
            get
            { return analyze_code; }

            set
            {
                if (analyze_code == value)
                { return; }

                analyze_code = value;
                RaisePropertyChanged("Code");
            }
        }

        private string analyze_label { get; set; }
        public string Label
        {
            get
            { return analyze_label; }

            set
            {
                if (analyze_label == value)
                { return; }

                analyze_label = value;
                RaisePropertyChanged("Label");
            }
        }

        private bool TO_DO { get; set; }
        public bool TODO
        {
            get
            { return TO_DO; }

            set
            {
                if (TO_DO == value)
                { return; }

                TO_DO = value;
                RaisePropertyChanged("TODO");
            }
        }

        #endregion Properties

        public ObservableCollection<ANALYSESViewModel> GetAnalyzeProductSelection(string PRD_CODE)
        {
            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<ANALYSESViewModel> re = db.Query<ANALYSESViewModel>(
                string.Format("SELECT DISTINCT A.AN_CODE AS Code, A.AN_LIBELLE AS Label, 1 AS TODO FROM ANAPRD P INNER JOIN ANALYSES A ON P.AS_ANA = A.AN_CODE WHERE P.AS_PROD='{0}'", PRD_CODE)
                );

            ObservableCollection<ANALYSESViewModel> result = new ObservableCollection<ANALYSESViewModel>();
            
            foreach (ANALYSESViewModel l in re)
            {
                result.Add(l);
            }

            return result;
        }

        public ObservableCollection<ANALYSESViewModel> GetAnalyzePlanSelection(string PC_CODE)
        {
            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<ANALYSESViewModel> re = db.Query<ANALYSESViewModel>(
                string.Format("SELECT DISTINCT A.AN_CODE AS Code, A.AN_LIBELLE AS Label, 1 AS TODO FROM ANAPLAN P INNER JOIN ANALYSES A ON P.AP_CODANA = A.AN_CODE WHERE P.AP_CODPLAN='{0}'", PC_CODE)
                );

            ObservableCollection<ANALYSESViewModel> result = new ObservableCollection<ANALYSESViewModel>();
            foreach (ANALYSESViewModel l in re)
            {
                result.Add(l);
            }

            return result;
        }

        public ObservableCollection<ANALYSESViewModel> GetAnalyzeSecteurSelection(string SE_CODE)
        {
            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<ANALYSESViewModel> re = db.Query<ANALYSESViewModel>(
                string.Format("SELECT DISTINCT A.AN_CODE AS Code, A.AN_LIBELLE AS Label, 0 AS TODO FROM ANALYSES A WHERE A.AN_SECTEUR='{0}'", SE_CODE)
                );

            ObservableCollection<ANALYSESViewModel> result = new ObservableCollection<ANALYSESViewModel>();
            foreach (ANALYSESViewModel l in re)
            {
                result.Add(l);
            }

            return result;
        }

        
    }
}
